<?php
/**
 * 产品文件
 *
 * @author lwb
 */

namespace app\wechat\controller;
use think\Log;

class Product extends Common {

    public function __construct() {
        parent::__construct();
    }

    /*
     * 检查商户
     */
    public function checkPartner($params){
//        return $this->parse_data(['code' => 5000, 'msg' => '系统升级中，请稍后再试！']);
        $result = $this->getWS('credit', 'product@checkPartner', ['partner_id' => $params['partner_id']]);
        if ($result['status'] != '2000') {
            return $this->parse_data(['code' => 5000, 'msg' => '机构编码不存在']);
        }

        session('partner_id', $result['result']['partner_id']);
        session('partner_name', $result['result']['partner_name']);
        session('partner_conf', $result['result']['partner_conf']);
        session('partner_k12', $result['result']['partner_k12']);
        session('min_age', $result['result']['partner_min_age']);
        session('max_age', $result['result']['partner_max_age']);
        session('p_id', null);
        session('p_name', null);
        //判断登陆，登陆了跳转课程列表页
        if(session('uid')){
//            $jump_url = '/user/authform';
            //修改流程
            $jump_url = "/productPay?partner_id=".base64_encode($result['result']['partner_id']).'&token_id='.substr(md5('MYBLJHZF'), 0, 16);
        }else{
            if(session('phone')){
                $jump_url = '/user/register?type=jiaoyu&phone='.session('phone');
            }else{
//                $jump_url = '/user/register?type=jiaoyu';
                //修改流程，跳转课程页面
                $jump_url = "/productPay?partner_id=".base64_encode($result['result']['partner_id']).'&token_id='.substr(md5('MYBLJHZF'), 0, 16);
            }
        }
        return $this->parse_data(['data' => ['jumpUrl' => $jump_url]]);
    }

    /**
     * 产品列表
     * add zenghu 2018-11-27 添加机构名称
     */
    public function productList($params){
        $result = $this->getWS('credit', 'product@productList', ['partner_id'=>session('partner_id'),'p_fund_type'=>$params['fundType']]);
        if ($result['status'] != '2000') {
            return $this->parse_data(['code' => 5000, 'msg' => '机构编码已失效']);
        }
        if($result['result']){
            $productList = [];
            foreach ($result['result'] as $v){
                $productList[trim($v['p_name'])]['p_name'] = $v['p_name'];
                $productList[trim($v['p_name'])]['p_id'] = $v['p_id'];
                $productList[trim($v['p_name'])]['p_set_amount'] = $v['p_set_amount'];
                $productList[trim($v['p_name'])]['p_name_text'] = $v['p_name_text'];
            }
        }
        return $this->parse_data(['data' => ['productList' => array_values($productList), 'partner_k12' => session('partner_k12'),'partner_name'=>session('partner_name'),'p_id'=>session('p_id'),'p_name'=>session('p_name'),'set_amount'=>session('set_amount')]]);
    }
    
    /*
     * 期数列表
     */
    public function periodList($params){
        $result = $this->getWS('credit', 'product@periodList', ['partner_id' => session('partner_id'), 'p_name' => $params['p_name']]);
        if ($result['status'] != '2000') {
            return $this->parse_data(['code' => 5000, 'msg' => '机构编码已失效']);
        }

        return $this->parse_data(['data' => $result['result']]);
    }

    /**
     * 获取资方类型、机构所支持的期数
     */
    public function getPeriodList($params){
        $result = $this->getWS('credit', 'product@getPeriodList', ['partner_id'=>session('partner_id'),'p_name'=>$params['p_name'],'fund_type'=>$params['fund_type']]);
        if ($result['status'] != '2000') {
            return $this->parse_data(['code' => 5000, 'msg' => '机构编码已失效']);
        }

        // 查询该课程是否已支付首付
        foreach($result['result'] as $key=>$val){
            $firstPaySuccess = $this->getWS('credit', 'Aggregate@getOrderList', ['u_code'=>$this->uid,'status'=>2000,'order_type'=>3,'product_id'=>$val['p_id'],'partner_id'=>session('partner_id')]);
            if($firstPaySuccess['status'] != '2000' || empty($firstPaySuccess['result'])){
                $result['result'][$key]['down_payment'] = 2; // 未支付首付
            }else{
                $result['result'][$key]['down_payment'] = 1; // 已支付首付
            }
        }

        return $this->parse_data(['data' => ['partner_name'=>session('partner_name'),'period'=>$result['result']]]);
    }

    /**
     * 获取产品配置（以课程名称、期数）
     */
    public function getProductDetail($params){
        $result = $this->getWS('credit', 'product@getProductDetail', ['p_name'=>$params['p_name'],'p_period'=>$params['p_period']]);
        if($result['status'] != '2000'){
            return $this->parse_data(['code'=>'5000', 'msg'=>'课程配置获取失败']);
        }

        return $this->parse_data(['data' => $result['result']]);
    }
    
    /*
     * 资方列表
     */
    public function fundList($params){
        $result = $this->getWS('credit', 'product@fundList', ['partner_id'=>session('partner_id'), 'period'=>$params['period']]);
        if ($result['status'] != '2000') {
            return $this->parse_data(['code' => 5000, 'msg' => '机构编码已失效']);
        }
        
        if($result['result']){
            foreach ($result['result'] as $k => &$v){
                 $v['fund_name'] = '通道'.($k+1);
            }
        }

        return $this->parse_data(['data' => $result['result']]);
    }

    /**
     * 出资方支持列表
     * add zenghu 2018年11月27日 
     */
    public function partnerFunds()
    {
        // 查询出资方支持列表
        $partnerFundList = $this->getWS('credit', 'product@partnerFundList', ['partner_id'=>session('partner_id')]);
        Log::write($partnerFundList, '查询出资方支持列表', true, 'partnerFundList/');
        if($partnerFundList['status'] != '2000'){
            return $this->parse_data(['code'=>5000,'msg'=>'资方支持列表查询失败']);
        }
        
        return $this->parse_data(['data'=>$partnerFundList['result']]);
    }

    /**
     * 获取产品详情
     * add zenghu 2018年11月28日 
     */
    // public function getProductDetail($params)
    // {
    //     // 判断参数
    //     if(empty($params['p_id'])){
    //         return $this->parse_data(['code'=>5000,'msg'=>'参数错误']);
    //     }

    //     // 获取产品详情
    //     $getProductDetail = $this->getWS('credit', 'product@getProductDetail', ['p_id'=>$params['p_id']]);
    //     if($getProductDetail['status'] != '2000'){
    //         return $this->parse_data(['code'=>5000,'msg'=>'获取产品详情失败']);
    //     }

    //     return $this->parse_data(['data'=>$getProductDetail['result']]);
    // }
 
}
